PHP BASICS


Opening and Closing


Just like HTML elements have opening and closing tags, PHP too has to be opened and closed. The most common syntax for opening and closing PHP is <?php to open and ?> to close.
<?php
 
//[all your yummy PHP goodness goes here]//
 
?>

Comments

comment.png
Comments in PHP are created in two different ways:
<?php
 
/*
Allows you to create a multiple line comment
*/
 
// Allows you to create a single line comment
 
?>
Comments are never read by PHP or by the web page, so go ahead and be verbose! Enter any information in here that will help you months from now remember what you were doing, and why you did it.

Variables

less1a.gif

Variables are a language construct in all computer languages that assigns your information to a container (like a box) that can be read in by the computer. This is like when you place items in your house into a box. You may have all kinds of stuffed animals, but they are placed into a box labeled toys. You then later know that toys = box of stuffed animals.

my box of toys has = rainbow brite, cabbage patch doll, snoopy
$toys = 'rainbow brite, cabbage patch doll, snoopy';

Variables in PHP start with the dollar sign ($) and then either a letter, or the underscore (_). After the first two characters, the variable name can contain any letters, numbers, or underscores.

  • Good:

    • $name
    • $_name
  • Bad:

    • $123 == cannot start with a number
    • $##%#@ == cannot have any punctuation other than the underscore

Variables are assigned information by using the equal sign "=". However, don't think this means equals , as confusing as this sounds, this sign means assigned to. Remember that for future reference!

After you assign your information to a computer variable, you will end using a semicolon (;). ALL statements in PHP end with a semicolon. You'll see examples below.

Types

Computer languages generally place different data into the type that defines it. For the purpose of this mini-course we will focus on two types "strings" and "integers".


Strings

g2567_fr.jpg

Strings are any amount of text, characters, punctuation, etc. that you want. Strings are usually written into the PHP code in either single or double quotes. Generally, if you do not have a PHP variable in your string, you will want to use single quotes. If you do have a PHP variable, use double quotes (and in some cases you would use single quotes and put the variable outside, but that is for another lesson!). Examples of strings assigned to a variable:

<?php
$name = 'Lisa Simpson'; // Don't forget the semicolon!
$age =  '10';
$paragraph = "Wow! This is a paragraph.
That's pretty cool."  ;
?>
Warning! Do you notice how I have a single quote inside of my double quotes? If you are using single quotes, you must use double quotes. If you are using double quotes, you must use single quotes. This gets very tricky when you actually have a variable inside of your variable... why? With a variable, you must use double quotes, or else PHP does not read it. PHP will not read information inside of single quotes. So what else can you do?

You have two options:

1) Escape
Escaping the quotes tells PHP that this quote is okay, just don't read it as a PHP quote. This is accomplished by using the backslash "\" . However, the disadvantage to this is that you may end up with lots of slashes all throughout your code and it will not look very nice, nor will it easy to go back and edit!

2) Concatenate!
According to the Merriam - Webster dictionary , concatenate means to "link together". To link together in PHP you would use the period (.). This allows you to put your own information in single quotes, add a variable, and then put double quotes inside. Like the image of the automobiles on a string above, concatenating allows you to put different variables together with strings, integers, or anything else you want.
Examples...
<?php
$sentence1 = 'This string is okay! I do not have any variables or double quotes,
              so I am using single quotes.';
 
/* $sentence2 = 'This string is NOT okay. I have a single quote, and PHP doesn't
              like single quotes in single quotes. I would have to use double quotes
              or use the backslash to escape.'; */
 
$sentence3 = "This string works much better with double quotes! Doesn't it?!";
 
//$sentence4 = 'This string works much better with slashes! Doesn\'t it?!';
 
$name = 'Lisa Simpson';
 
$sentence5 = 'This string will display $name exactly how you see it. I am using
              single quotes, so "$name" will not change to Lisa Simpson. PHP does not
              read single quotes.';
 
$sentence6 = "This string will display $name as 'Lisa Simpson'!";
 
$sentence7 = 'BUT, this string will work as well, and will change ' . $name . '
              to Lisa Simpson!';
 
?>
Notice in $sentence7 that there is a period on both the left and right side of the variable. Also notice that $name is NOT in quotes at all. PHP reads this and displays it as:
"BUT, this string will work as well, and will change Lisa Simpson to Lisa Simpson"

Look over these carefully. Don't worry about doing it for now, you will on the next page and then it will click!

Note - I have used comments on the $sentence1 because it is incorrect, and $sentence4 because the built in color editor for this page does not see the backslash - and therefore makes it incorrect. Displaying these would throw off the color formatting which makes it easier for you to see the variables!

Integers

numbers_12_.jpg
Integers in PHP are whole numbers such as -1, 10000, 1, and 0. The difference between a string with a number in it like "10" and the number 10 is that you are able to do mathematics on an integer. But take note that PHP will also automagically change "10" to an integer for you, if "10" does not have any other text or punctuation. PHP is just really cool like that!

Integers are not in quotes, but are assigned to variables in the same way as a string.

<?php
$string = '10';
$integer = 10;
 
$sum = $string + $integer;
echo $sum; //Displays 10
?>
Whoa! What went on here?
First we assign the variable named $string the value of '10' - as a string. We then assigned $integer the value of 10 as an integer.

We then created an entirely new variable named $sum and we added (+) $string and $integer. We then used an new word for you - echo**.

Echo displays whatever you tell it to, to the actual computer screen. Echo is your friend. Echo any variable you want!
echo.jpg

So, we ended up "echoing" out the variable $sum. This displays 10.

$sum = $string + $integer;
really means
$sum = 10 + 10;


Get it? No? Yes? Not so much? It is okay =). Let us go on to making our first page where it will become crystal clear!